package com.mobile.challenge.ui.home

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.mobile.challenge.api.SignInService
import com.mobile.challenge.api.UserOneDayDataService
import com.mobile.challenge.bean.TrackingBean
import com.mobile.challenge.exception.MobileException
import com.mobile.challenge.user.UserHelper
import com.mobile.challenge.util.DateUtil
import com.mobile.challenge.util.SUCCESS_ERROR_CODE
import com.mobile.challenge.util.SUCCESS_ERROR_MSG
import com.mobile.frame.mvvmframe.base.BaseModel
import com.mobile.frame.mvvmframe.base.DataViewModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.*
import javax.inject.Inject

class HomeViewModel
@Inject constructor(application: Application, model: BaseModel?)
    : DataViewModel(application, model) {

    var signInData: MutableLiveData<TrackingBean> = MutableLiveData()
        internal set

    var signInError: MutableLiveData<MobileException> = MutableLiveData()
        internal set

    var signInStatus = MutableLiveData<Int>()
        internal set

    var tracking: TrackingBean? = null
        internal set

    fun updateSignIn(userId: String, status: String, date: String) {
        if (tracking != null) {
            updateSignIn(status)
        } else {
            signIn(userId, status, date)
        }
    }

    private fun updateSignIn(status: String) {
        val retrofitService = getRetrofitService(SignInService::class.java)
        val disposable = retrofitService.updateSignIn(tracking!!.id, status)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ response ->
                    if (response.errorCode == SUCCESS_ERROR_CODE && response.errorMsg == SUCCESS_ERROR_MSG) {
                        signInData.value = response.data
                        UserHelper.getInstance().saveTracking(response.data)
                    } else {
                        val exception = MobileException()
                        exception.errorCode = response.errorCode
                        exception.errorMsg = response.errorMsg
                        signInError.value = exception
                    }

                }, {
                    val exception = MobileException()
                    exception.errorCode = "9999"
                    exception.errorMsg = it.message
                    exception.hintMsg = "please try again later "
                    signInError.value = exception
                })
    }

    private fun signIn(userId: String, status: String, date: String) {
        val retrofitService = getRetrofitService(SignInService::class.java)
        val disposable = retrofitService.signIn(userId, status, date)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ response ->
                    if (response.errorCode == SUCCESS_ERROR_CODE && response.errorMsg == SUCCESS_ERROR_MSG) {
                        signInData.value = response.data
                    } else {
                        val exception = MobileException()
                        exception.errorCode = response.errorCode
                        exception.errorMsg = response.errorMsg
                        signInError.value = exception
                    }

                }, {
                    val exception = MobileException()
                    exception.errorCode = "9999"
                    exception.errorMsg = it.message
                    exception.hintMsg = "please try again later "
                    signInError.value = exception
                })
    }

    fun requestSignInStatus(calendar: Calendar) {
        val subscribe = getRetrofitService(UserOneDayDataService::class.java)
                .getUserDataByOneDay(UserHelper.getInstance().getUserId(),
                        DateUtil.getFormatDateStr(calendar))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ response ->
                    if (response.errorCode == SUCCESS_ERROR_CODE && response.errorMsg == SUCCESS_ERROR_MSG) {
                        val data = response.data
                        tracking = response.data
                        // 数据
                        if (data != null) {
                            signInStatus.value = data.status
                        } else {
                            signInStatus.value = -1
                        }
                    } else {
                        val exception = MobileException()
                        exception.errorCode = response.errorCode
                        exception.errorMsg = response.errorMsg

                        signInStatus.value = -1
                    }
                }, {
                    val exception = MobileException()
                    exception.errorCode = "00000"
                    exception.errorMsg = "response.errorMsg"

                    signInStatus.value = -1
                })
    }

}